package com.enniswu.weitter.moment.posting;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.enniswu.weitter.R;
import com.enniswu.weitter.base.BaseActivity;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.widget.adapter.AlbumAdapter;
import com.enniswu.weitter.widget.adapter.CommentAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class PostingActivity extends BaseActivity<PostingContract.Presenter> implements PostingContract.View, OnLoadMoreListener {

    @BindView(R.id.rl_post)
    SmartRefreshLayout rlPost;
    @BindView(R.id.iv_portrait)
    ImageView ivPortrait;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_variety)
    TextView tvVariety;
    @BindView(R.id.tv_context)
    TextView tvContext;
    @BindView(R.id.rv_album)
    RecyclerView rvAlbum;
    @BindView(R.id.rv_comment)
    RecyclerView rvComment;
    @BindView(R.id.et_context)
    EditText etContext;
    private Post post;
    private List<Post> comments;
    private CommentAdapter cAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posting);
        setTitle(R.string.posting_title);
        post = getIntent().getParcelableExtra("post");
        User user = DBSupport.getInstance().findUser(post.getUserId(), false);
        rlPost.setEnableRefresh(false);
        rlPost.setOnLoadMoreListener(this);
        if (user != null) {
            Glide.with(this).load(user.getPortrait())
                    .apply(new RequestOptions().circleCrop()).into(ivPortrait);
            tvName.setText(user.getName());
        }
        tvVariety.setText(post.getCreateTime());
        tvContext.setText(post.getContext());
        AlbumAdapter aAdapter = new AlbumAdapter(post.getAlbum());
        rvAlbum.setNestedScrollingEnabled(false);
        rvAlbum.setLayoutManager(new GridLayoutManager(this, 3));
        rvAlbum.setAdapter(aAdapter);
        comments = Collections.synchronizedList(new ArrayList<Post>());
        cAdapter = new CommentAdapter(comments);
        rvComment.setNestedScrollingEnabled(false);
        rvComment.setLayoutManager(new LinearLayoutManager(this));
        rvComment.setAdapter(cAdapter);
        new PostingPresenter(this, 1, post.getId());
        presenter.getPosts(1);
    }

    @Override
    public void notifyDataSetChanged() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                cAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void notifyDataInserted(final int count) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                cAdapter.notifyItemRangeInserted(comments.size() - count, count);
                cAdapter.notifyItemRangeChanged(comments.size() - count, count);
            }
        });
    }

    @Override
    public void notifyItemUserChanged(Post post) {
        final int index = comments.indexOf(post);
        if (index < 0)
            return;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                cAdapter.notifyItemChanged(index);
            }
        });
    }

    @Override
    public void addPosts(List<Post> posts) {
        comments.addAll(posts);
    }

    @Override
    public void clearPosts() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void finishLoadData() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rlPost.finishLoadMore();
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        presenter.getPosts(comments.size() + 1);
    }

    @Override
    public void next(Post post) {
        addPosts(Collections.singletonList(post));
        notifyDataInserted(1);
    }

    @OnClick(R.id.iv_send)
    public void onViewClicked() {
        presenter.comment(etContext.getText().toString().trim());
        etContext.getText().clear();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null)
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
    }
}
